package com.panfeng.xcloud.boss.provider.assets.service.impl;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.CoinListMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.OffsetTransFlowMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dto.OffsetTransFlowExtendItemDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.request.QueryOffsetTransFlowPageReqDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.response.QueryOffsetTransFlowPageRespDTO;
import com.panfeng.xcloud.boss.provider.assets.service.IOffsetTransFlowService;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.dao.assets.entity.CoinList;
import com.panfeng.xcloud.dao.assets.entity.OffsetTransFlow;
import com.panfeng.xcloud.dao.base.MapperSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OffsetTransFlowServiceImpl extends MapperSupport<Integer, OffsetTransFlow, OffsetTransFlowMapperExt> implements IOffsetTransFlowService {

    @Autowired
    OffsetTransFlowMapperExt offsetTransFlowMapperExt;

    @Autowired
    CoinListMapperExt coinListMapperExt;

    /**
     * 查询对冲交易记录
     * @param queryOffsetTransFlowPageReqDTO
     * @return
     */
    @Override
    public PageResponse<QueryOffsetTransFlowPageRespDTO> queryOffsetTransFlow(QueryOffsetTransFlowPageReqDTO queryOffsetTransFlowPageReqDTO) {
        PageResponse<OffsetTransFlowExtendItemDTO> pageResponse = super.selectPage(queryOffsetTransFlowPageReqDTO, offsetTransFlowMapperExt::queryOffsetTransFlowExtPageCount, offsetTransFlowMapperExt::queryOffsetTransFlowExtPage);

        List<OffsetTransFlowExtendItemDTO> items = pageResponse.getItems();
        log.info(">>>> queryOffsetTransFlow-items.list：{} <<<",(null != items ? JSON.toJSONString(items):""));
        List<QueryOffsetTransFlowPageRespDTO> queryOffsetTransFlowRespDTOS = ConvertUtils.convert(items, QueryOffsetTransFlowPageRespDTO.class);

        List<QueryOffsetTransFlowPageRespDTO> result = new ArrayList<>();

        for (QueryOffsetTransFlowPageRespDTO queryOffsetTransFlowRespDTO : queryOffsetTransFlowRespDTOS) {
            CoinList coinList = coinListMapperExt.queryCoinListByDestinationCoinAndSourceCoin(queryOffsetTransFlowRespDTO.getExchangeType(),
                    queryOffsetTransFlowRespDTO.getDestinationCoin(),queryOffsetTransFlowRespDTO.getSourceCoin(), null,
                    queryOffsetTransFlowRespDTO.getDirection());
            queryOffsetTransFlowRespDTO.setUrl(coinList.getUrl());
            result.add(queryOffsetTransFlowRespDTO);
        }

        PageResponse<QueryOffsetTransFlowPageRespDTO> pageFinalResponse = new PageResponse();
        pageFinalResponse.setTotalCount(pageResponse.getTotalCount());
        pageFinalResponse.measureTotalPage(pageResponse.getTotalCount(),pageResponse.getPageSize());
        pageFinalResponse.setItems(result);
        pageFinalResponse.setPageNumber(pageResponse.getPageNumber());
        pageFinalResponse.setPageSize(pageResponse.getPageSize());

        return pageFinalResponse;
    }

}
